<?php

class Tuoshuo_magage  extends AdminManage
{
	private $_pagesize=10;//分页个数
	/**
	 * 初始化
	 */
	public function _init()
	{

	}

	/**
	 * 默认动作
	 */
	protected function _default()
	{

		$strWhere=' 1 ';
//		$strWhere.=' and  checkState !=1 ';
		
		#查询条件处理-start
		 $timeStart = $this->getParam('jts');
         $timeEnd   = $this->getParam('jte');
		 $state   = $this->getParam('state');
		 $hot   = $this->getParam('hot');
		 $keyword   = $this->getParam('kw');
		#查询条件处理-end
		
		$datelinesStamp = strtotime($timeStart);
		$datelinedStamp = strtotime($timeEnd." 23:59:59");
			
	    if($datelinesStamp) {
			$strWhere .= ' and dateline >= ' . $datelinesStamp;
			$this->assign('jts',$timeStart);
		}
		if($datelinedStamp) {
			$strWhere .= ' and dateline <= ' . $datelinedStamp;
			$this->assign('jte',$timeEnd);
		}
		
		
		if(is_numeric($state) && in_array($state,array(0,1,2))){
			$strWhere .= ' and checkState  = ' . $state;
			$this->assign('state',$state==2?'pass':'nopass');
		}
 
 
		if(!empty($keyword)){
		    $kwlen = strlen($keyword);
			$strWhere .= " and `describe`  like '%" . $keyword."%'";
			$this->assign('kw',$keyword);
		}

		#获取数据总数
		$total = DB::getRows('tushuo',$strWhere);


		/**分页操作 start**/
		$p = 1; //当前页码
		if (isset($_GET['p']) && is_numeric($_GET['p'])) {
			$p =intval($_GET['p']);
			if ($p<=1) {
				$p = 1;
			}
		}

		$totalPage = ceil($total/$this->_pagesize);//计算总页数
		$begin = ($p-1)*$this->_pagesize;//计算开始游标
		$limit = "$begin,$this->_pagesize";//计算分页


        if($totalPage > 0){
		  $pageObj = new Dpage($total, $this->_pagesize, $p);
		  $pageCss = $pageObj->pageCss("1");
		  $pageform = $pageObj->outputPages(1,"<li>","</li>");
		}

		/**分页操作 end**/


		#获取数据信息
		$list = new publicGetDataModel();
		$listData = $list->getData($where = $strWhere,$limit = $limit,$fields = '`describe`,img,checkState,dateline',$order = 'dateline',$table='tushuo');

		if(empty($listData)) {
			if (isset($key)) {
				$nodatainfo = '没有找到相匹配的图说发布信息';
			}
		}


		if($listData){
			foreach($listData  as $k=>$v){
				 #状态
				if($v['CHECKSTATE']==2){
					$listData[$k]['STATENAME'] = '<span class="status-normal">上架</span>';
				}elseif($v['CHECKSTATE']==1){
					$listData[$k]['STATENAME'] = '<span class="status-cancel">下架</span>';
				}
			}
		}


		#模板赋值
		$now=time();
		$this->assign('now',$now);
		$this->assign('classify_list',$listData);
		$this->assign('pageCss',$pageCss);//分页样式
		$this->assign('pageform',$pageform);//分页标签
		$this->assign('type',$type);
		$this->assign('kw',$key);
		$this->assign('nodatainfo',$nodatainfo);
 
		$this->assign('menu_navbar','内容管理  -- <a href="'.$_GET['action'].'.html">图说管理列表</a>');
		$this->tpl_full_path('/admins/tuoshuo_magage/index');
	}




	/**
	 **发布
	 **/
	public function _increase()
	{
		$id = $this->getParam('id','i');

		if($_SERVER["REQUEST_METHOD"] === "POST"){
			
 
			$info = $this->getParam('info');
 
			if(empty($info)){
				$this->alertBack("描述不能空,请重新输入！");
			}
			
			$data['describe'] = $info;

			//图片处理
			if(is_array($_REQUEST['imgup_imgfiles'])&&count($_REQUEST['imgup_imgfiles']>0)){
				$data['img']=join(',',$_REQUEST['imgup_imgfiles']);
			}

			$data['dateline']  = time();
 

			if($id>0){ //编辑
                if(DB::update("tushuo",'id='.$id,$data)){
					$message='编辑成功！';
					$url='http://system.gzcanr.com/admins/tuoshuo_magage.html';
					$this->alertGoto($message,$url);
				}else{
					$this->alertBack("编辑失败,请重试！");
				}
			}else{
				$data['checkState'] = 2;
				if(DB::insert("tushuo",$data)){
					$message='保存成功！';
					$url='http://system.gzcanr.com/admins/tuoshuo_magage.html';
					$this->alertGoto($message,$url);
				}else{
					$this->alertBack("保存失败,请重试！");
				}

			}

		}//end 编辑发布提交表单


		if($id) {  //编辑
			$res = DB::select('select * from tushuo where id ='.$id );

			//图片
			$oldimg = '';
			if(!empty($res[0]['IMG'])) {

				$res[0]['IMG']=explode(',',$res[0]['IMG']);//拆分图片路径

				foreach($res[0]['IMG']   as $k=>$v   ){
					$oldimg['list'][$k]['filepath'] = Func::imageSrcHandle($v,'z','85*85','1');
					$oldimg['list'][$k]['id'] = $k;
				}

				$oldimg['img_cnt_limit'] = 1;
				$imgjs = getUploadImgCode('imgup','banner',$oldimg);
				$this->assign('imgjs',$imgjs);

			}
 

			$this->assign('data',$res[0]);
		}



		$this->assign('menu_navbar','内容管理  -- <a href="'.$_GET['action'].'.html">图说发布发布</a>');
		$this->tpl_full_path('/admins/tuoshuo_magage/add_form');
	}
 
	
	
	
	/**
	 * 状态操作
	 */
	protected function _dostate()
	{
		$id = $this->getParam('id');
		$type = $this->getParam('type');
		if(empty($id) && empty($type)){
			die('0');
		}
		$arrUpdate = array();
		if($type == 'pass'){
			$arrUpdate['checkState'] = 2;
		}else{
		    $arrUpdate['checkState'] = 1;
		}
//		$arrUpdate['dateline'] = time();
		$updateStatus = DB::update('news','id = '.$id,$arrUpdate);
		if($updateStatus){
		   die('1');	
		}else{
		   die('0');
		}
		
	}
	
	
	
	/**
	 * 热门操作
	 */
	protected function _dohot()
	{
		$id = $this->getParam('id');
		$type = $this->getParam('type');
		if(empty($id) && empty($type)){
			die('0');
		}
		$arrUpdate = array();
		if($type == 'hot'){
			$arrUpdate['hot'] = 1;
		}else{
		    $arrUpdate['hot'] = 0;
		}
//		$arrUpdate['dateline'] = time();
		$updateStatus = DB::update('news','id = '.$id,$arrUpdate);
		if($updateStatus){
		   die('1');	
		}else{
		   die('0');
		}
		
	}
	
	







}
?>
